﻿namespace zyn_hy_login
{
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    public class zyn2
    {
        private string[] ActionSafe;
        private string[] AttributeSafe;
        private bool result;
        protected string strConn;
        protected static string strSQL;

        public zyn2()
        {
            this.strConn = ConfigurationSettings.AppSettings["connectString"];
        }

        public zyn2(string[] safe_action)
        {
            this.strConn = ConfigurationSettings.AppSettings["connectString"];
            this.ActionSafe = safe_action;
            this.zyn2SeparateSafe();
        }

        private bool Check_FolderSafe(string[] FolderSafe)
        {
            FolderSafe[0] = new SecurityZ8().Decrypt(FolderSafe[0]);
            switch (FolderSafe[1])
            {
                case "R":
                    if (FolderSafe[0].Substring(0, 1) == "1")
                    {
                        this.result = false;
                    }
                    else
                    {
                        this.result = true;
                    }
                    break;

                case "D":
                    if (FolderSafe[0].Substring(1, 1) == "1")
                    {
                        this.result = false;
                    }
                    else
                    {
                        this.result = true;
                    }
                    break;

                case "W":
                    if (FolderSafe[0].Substring(2, 1) == "1")
                    {
                        this.result = false;
                    }
                    else
                    {
                        this.result = true;
                    }
                    break;

                case "C":
                    if (FolderSafe[0].Substring(3, 1) == "1")
                    {
                        this.result = false;
                    }
                    else
                    {
                        this.result = true;
                    }
                    break;
            }
            return this.Result;
        }

        public bool CheckFolderSafe(string[] CheckString)
        {
            switch (CheckString[2])
            {
                case "file":
                    if ((CheckString[0] != "") && (CheckString[0].Length >= 8))
                    {
                        string[] stra = new string[2];
                        stra[1] = CheckString[3];
                        stra[0] = CheckString[0];
                        this.CheckSafe(stra);
                        goto Label_00B2;
                    }
                    this.result = false;
                    break;

                case "folder":
                    if ((CheckString[1] == "") || (CheckString[1].Length < 8))
                    {
                        this.result = false;
                    }
                    else
                    {
                        string[] folderSafe = new string[2];
                        folderSafe[1] = CheckString[3];
                        folderSafe[0] = CheckString[1];
                        this.Check_FolderSafe(folderSafe);
                    }
                    goto Label_00B2;
            }
        Label_00B2:
            return this.Result;
        }

        public bool CheckSafe(string[] stra)
        {
            this.ActionSafe = stra;
            this.zyn2SeparateSafe();
            return this.Result;
        }

        public string GetCommonZYN(string strId, string strId2, string strId3)
        {
            SqlConnection connection = new SqlConnection(this.strConn);
            strSQL = "select x50 from z8 where id=" + strId + " and id2=" + strId2 + " and id3=" + strId3 + "";
            SqlCommand command = new SqlCommand(strSQL, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                reader.Close();
                return reader.GetString(0);
            }
            reader.Close();
            return "";
        }

        private void zyn2SeparateSafe()
        {
            SecurityZ8 yz = new SecurityZ8();
            string text = yz.Decrypt(this.ActionSafe[0]);
            if (text == "")
            {
                this.result = false;
            }
            else
            {
                text = text.Replace("0", "false").Replace("*", "true");
                char[] separator = new char[] { '/' };
                this.AttributeSafe = new string[yz.Decrypt(this.ActionSafe[0]).Split(separator).Length];
                this.AttributeSafe = text.Split(separator);
                switch (this.ActionSafe[1])
                {
                    case "R":
                        if (this.AttributeSafe[0] == "true")
                        {
                            this.result = true;
                        }
                        else
                        {
                            this.result = false;
                        }
                        break;

                    case "W":
                        if (this.AttributeSafe[2] == "true")
                        {
                            this.result = true;
                        }
                        else
                        {
                            this.result = false;
                        }
                        break;

                    case "A":
                        if (this.AttributeSafe[4] == "true")
                        {
                            this.result = true;
                        }
                        else
                        {
                            this.result = false;
                        }
                        break;

                    case "D":
                        if (this.AttributeSafe[6] == "true")
                        {
                            this.result = true;
                        }
                        else
                        {
                            this.result = false;
                        }
                        break;

                    case "E":
                        if (this.AttributeSafe[8] == "true")
                        {
                            this.result = true;
                        }
                        else
                        {
                            this.result = false;
                        }
                        break;

                    case "RWADE":
                        if ((((this.AttributeSafe[0] == "true") && (this.AttributeSafe[2] == "true")) && ((this.AttributeSafe[4] == "true") && (this.AttributeSafe[6] == "true"))) && (this.AttributeSafe[8] == "true"))
                        {
                            this.result = true;
                        }
                        else
                        {
                            this.result = false;
                        }
                        break;
                }
            }
        }

        public bool Result
        {
            get
            {
                return this.result;
            }
        }
    }
}

